﻿using System;
using System.Windows;
using System.Windows.Media;
using ColorWheel.Classes;

namespace ColorWheel
{
    public partial class Page
    {
        private const int SliceCount = 36;                
        private const double ByteStep = 42.5;

        private const int StartRed = 0;
        private int RedStep;
        private double RedValue;

        private const int BlueStart = 12;
        private int BlueStep = 0;
        private double BlueValue = 0;

        private const int GreenStart = 24;
        private int GreenStep = 13;
        private double GreenValue = 255;

        public Page()
        {
            InitializeComponent();

            Loaded += Page_Loaded;
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            Color activeColor = new Color();
            activeColor.A = 255;
            activeColor.R = 0;
            activeColor.G = 0;
            activeColor.B = 0;

            for (int sliceNumber = 0; sliceNumber < SliceCount; sliceNumber++)
            {
                ColorSlice colorSlice = new ColorSlice();
                activeColor.R = CalcColorByte(sliceNumber, StartRed, ref RedStep, ref RedValue);
                activeColor.G = CalcColorByte(sliceNumber, GreenStart, ref GreenStep, ref GreenValue);
                activeColor.B = CalcColorByte(sliceNumber, BlueStart, ref BlueStep, ref BlueValue);
                colorSlice.SetColor(activeColor);

                Point origin = new Point(0.5,1);
                colorSlice.SetValue(RenderTransformOriginProperty,origin);
                
                TransformGroup transformGroup = new TransformGroup();
                RotateTransform rotation = new RotateTransform();
                rotation.Angle = sliceNumber*10;
                transformGroup.Children.Add(rotation);
                colorSlice.RenderTransform = transformGroup;

                colorSlice.SetValue(VerticalAlignmentProperty,VerticalAlignment.Top);
                Thickness margin = new Thickness(0,3,0,0);
                colorSlice.SetValue(MarginProperty, margin);

                colorSlice.ColorClick += colorSlice_ColorClick;

                WheelHouse.Children.Add(colorSlice);
            }
        }

        void colorSlice_ColorClick(object sender, SliceEventArgs e)
        {
            ColorBox1.Fill = new SolidColorBrush(e.ActiveColor);
        }

        private byte CalcColorByte(int CurrentSlice, int ColorStart, ref int ColorStep, ref double ColorValue)
        {
            if (CurrentSlice >= ColorStart || ColorStep > 0)
            {
                if (ColorStep >= 18)
                {
                    ColorValue -= ColorStep;
                }
                else
                {
                    ColorValue = ColorStep * ByteStep;
                }

                if (ColorValue > 255)
                {
                    ColorValue = 255;
                }
                else if (ColorValue < 0)
                {
                    ColorValue = 0;
                }

                if (ColorStep >= 24)
                {
                    ColorStep = 0;
                    ColorValue = 0;
                }
                else
                {
                    ColorStep++;
                }
            }

            return Convert.ToByte(ColorValue);
        }
    }
}
